$(function() {
    //主流程对象
    var mainCtrl = avalon.define({
        $id: "mainController",
        staffId: "",
        addBlackPop: function() {
            popCtrl.staffId = "";
            popCtrl.staffName = "";
            popCtrl.sendTimes = "";
            popCtrl.reason = "";
            $("textarea.reason").val("");
            $(".u-error").hide();
            popCtrl.noStaffName = false;
            $("#popup_add").show();
        },
        searchBlackList: function() {
            var searchParam = {
                "pageNo": 1,
                "pageSize": 10,
                "staffId": mainCtrl.staffId
            }
            taskTable.search(searchParam);
        }
    });

    //弹框对象
    var popCtrl = avalon.define({
        $id: "popController",
        delstaffId: "",
        staffId: "",
        staffName: "",
        sendTimes: "",
        reason: "",
        noStaffName: false,
        confirmSure: function() {
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "staffId": popCtrl.delstaffId
                },
                url: DI.deleteBlack,
                success: function(data) {
                    $("#popup_confirm").hide();
                    if(data.retFlag == 1) {
                        PLATFORM.showTips("success", "删除成功");
                        taskTable.searchParam.pageNo = "1";
                        taskTable.refresh();
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        },
        searchstaffName: function() {
            if(popCtrl.staffId.length != 8) {
                return;
            }
            $(".u-error.staffid").hide();
            var reg = /^\d{8}$/;
            if(!reg.test(popCtrl.staffId)) {
                $(".u-error.staffid").text("工号为8位数字").show();
                return;
            }
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "staffId": popCtrl.staffId
                },
                url: DI.queryStaffName_innerCoupon,
                success: function(response) {
                    if(response.retFlag == 1) {
                        var data = response.data;
                        $(".u-error.staffid").hide();
                        popCtrl.staffName = data.staffName;
                        popCtrl.sendTimes = data.sendTimes;
                        popCtrl.noStaffName = false;
                    } else {
                        popCtrl.staffName = "";
                        popCtrl.sendTimes = "";
                        popCtrl.noStaffName = true;
                        $(".u-error.staffid").text(response.errorMessage).show();
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        },
        checkIpt: function() {
            if(popCtrl.noStaffName) {
                return true;
            }

            $(".u-error").hide();

            if(popCtrl.staffId == "") {
                $(".u-error.staffid").text("工号不能为空").show();
                return true;
            } else {
                var reg = /^\d{8}$/;
                if(!reg.test(popCtrl.staffId)) {
                    $(".u-error.staffid").text("工号为8位数字").show();
                    return true;
                }
            }
            if($("textarea.reason").val() == "") {
                $(".u-error.reason").text("原因不能为空").show();
                return true;
            }
            if($(".u-error").css("display") != "none") {
                return true;
            }
            return false;
        },
        addSure: function() {
            if(popCtrl.checkIpt()) {
                return;
            }
            $.ajax({
                type: "post",
                dataType: "json",
                data: {
                    "staffId": popCtrl.staffId,
                    "staffName": popCtrl.staffName,
                    "reason": $("textarea.reason").val()
                },
                url: DI.addBlack,
                success: function(data) {
                    if(data.retFlag == 1) {
                        $("#popup_add").hide();
                        PLATFORM.showTips("success", "新增成功");
                        taskTable.searchParam.pageNo = "1";
                        taskTable.refresh();
                    } else {
                        PLATFORM.showTips("error", data.errorMessage);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });
        }
    });

    //表格实例化
    var taskTable = null;
    var opts = {
        targetId: "taskTable",
        templateId: "tableTask",
        settings: {
            url: DI.queryBlackList,
            ajaxType: "json",
            sourceInterface: {
                INum: "totalSize",
                IData: "records"
            },
            paginationIndexName: {
                "peerpageRows": "pageSize",
                "currentPage": "pageNo"
            },
            searchParam: {
                "staffId": ""
            }
        },
        customFuncs: {
            delBlackPop: function(delstaffId) {
                popCtrl.delstaffId = delstaffId;
                $("#popup_confirm").show();
            }
        }
    };
    taskTable = SwiftUI.swiftTable.getInstance(opts);

    avalon.scan();
});